//
//  XQSmallCardCell.swift
//  Spark Peso
//
//  Created by 小琦 on 2025/2/3.
//

import UIKit

class XQSmallCardCell: UITableViewCell {
    
    var model : XQHomeReadingCaucasianModel? {
        didSet {
            if let url = model?.coarseness {
                self.logoImageView.kf.setImage(with: URL(string: url))
            }
            self.titleLb.text = model?.flashlight ?? ""
            self.dollorLb.text = model?.pencil ?? ""
            
            self.termView.title = model?.satirized ?? ""
            self.termView.content = model?.guidelines ?? ""
            
            self.rateView.title = model?.lee ?? ""
            self.rateView.content = model?.phrase ?? ""
            
            self.btn.setTitle(model?.tested ?? "", for: .normal)
        }
    }
    
    private lazy var logoImageView : UIImageView = {
        let logo = UIImageView()
        logo.layer.cornerRadius = 6
        return logo
    }()
    
    private lazy var titleLb : UILabel = {
        let lb = UILabel()
        lb.font = UIFont.systemFont(ofSize: 15, weight: .medium)
        lb.textColor = .xq_black
        return lb
    }()
    
    private lazy var dollorLb : UILabel = {
        let lb = UILabel()
        lb.font = UIFont.systemFont(ofSize: 22, weight: .bold)
        lb.textColor = .xq_black
        lb.adjustsFontSizeToFitWidth = true
        return lb
    }()
    
    private lazy var termView : XQSmallBetweenView = {
        let v = XQSmallBetweenView()
        return v
    }()
    
    private lazy var rateView : XQSmallBetweenView = {
        let v = XQSmallBetweenView()
        return v
    }()
    
    private lazy var btn : UIButton = {
        let btn = UIButton(type: .custom)
        btn.backgroundColor = .mainTextColor
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 11,weight: .medium)
        btn.titleLabel?.textColor = .white
        btn.layer.cornerRadius = 13.5
        btn.isEnabled = false
        return btn
    }()
    
    private lazy var lineView : UIView = {
        let v = UIView()
        v.backgroundColor = UIColor(hex: "#EFEFEF")
        return v
    }()
    
    var firstRow : Bool = false {
        didSet {
            self.logoImageView.snp.updateConstraints { make in
                make.top.equalTo(firstRow ? 0 : 16)
            }
        }
    }
    
    var lastRow : Bool = false {
        didSet {
            self.lineView.isHidden = lastRow
        }
    }

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
       super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
        self.makeUI()
        self.makeConstraints()
    }

    required init?(coder: NSCoder) {
       fatalError("init(coder:) has not been implemented")
    }
    
    func makeUI() {
        self.contentView.addSubview(self.logoImageView)
        self.contentView.addSubview(self.titleLb)
        self.contentView.addSubview(self.dollorLb)
        self.contentView.addSubview(self.termView)
        self.contentView.addSubview(self.rateView)
        self.contentView.addSubview(self.btn)
        self.contentView.addSubview(self.lineView)
    }
    
    func makeConstraints() {
        self.logoImageView.snp.makeConstraints { make in
            make.top.equalTo(16)
            make.left.equalTo(10)
            make.width.height.equalTo(30)
        }
        self.titleLb.snp.makeConstraints { make in
            make.centerY.equalTo(self.logoImageView)
            make.left.equalTo(self.logoImageView.snp.right).offset(5)
        }
        self.dollorLb.snp.makeConstraints { make in
            make.right.equalTo(-10)
            make.centerY.equalTo(self.logoImageView)
            make.left.greaterThanOrEqualTo(self.titleLb.snp.right).offset(12)
        }
        self.termView.snp.makeConstraints { make in
            make.top.equalTo(self.logoImageView.snp.bottom).offset(12)
            make.left.equalTo(self.logoImageView.snp.left)
        }
        self.rateView.snp.makeConstraints { make in
            make.left.equalTo(self.logoImageView.snp.left)
            make.top.equalTo(self.termView.snp.bottom).offset(8)
        }
        self.btn.snp.makeConstraints { make in
            make.bottom.equalTo(self.rateView.snp.bottom)
            make.right.equalTo(-10)
            make.width.equalTo(87)
            make.height.equalTo(27)
        }
        self.lineView.snp.makeConstraints { make in
            make.bottom.equalTo(0)
            make.left.equalTo(10)
            make.right.equalTo(-10)
            make.height.equalTo(1)
        }
    }

}
